#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
简化的图表生成模块
"""

def create_simple_chart(data, title):
    """创建简单的文本图表"""
    if not data or not isinstance(data, list):
        return None
    
    # 提取数据
    labels = []
    values = []
    
    for item in data:
        if not isinstance(item, dict):
            continue
            
        # 提取标签
        label = None
        if '股票名称' in item:
            label = item['股票名称']
        elif '行业名称' in item:
            label = item['行业名称']
        
        # 提取数值
        value = None
        if '联动系数' in item:
            value = float(item['联动系数'])
        elif '综合评分' in item:
            value = float(item['综合评分'])
        elif '平均成交额（亿元）' in item:
            value = float(item['平均成交额（亿元）'])
        elif '相关度分数' in item:
            value = float(item['相关度分数'])
        
        if label and value is not None:
            labels.append(label)
            values.append(value)
    
    if len(labels) < 2:
        return None
    
    # 创建简单的文本图表
    chart_text = f"\n{title}\n"
    chart_text += "=" * 50 + "\n"
    
    max_value = max(values)
    max_label_len = max(len(label) for label in labels)
    
    for i, (label, value) in enumerate(zip(labels, values)):
        bar_length = int((value / max_value) * 30) if max_value > 0 else 0
        bar = "█" * bar_length
        chart_text += f"{label.ljust(max_label_len)} | {bar} {value:.3f}\n"
    
    chart_text += "=" * 50 + "\n"
    
    return chart_text

def create_chart_svg(data, title):
    """创建SVG格式的简单图表"""
    if not data or not isinstance(data, list):
        return None
    
    # 提取数据
    labels = []
    values = []
    
    for item in data:
        if not isinstance(item, dict):
            continue
            
        # 提取标签
        label = None
        if '股票名称' in item:
            label = item['股票名称']
        elif '行业名称' in item:
            label = item['行业名称']
        
        # 提取数值
        value = None
        if '联动系数' in item:
            value = float(item['联动系数'])
        elif '综合评分' in item:
            value = float(item['综合评分'])
        elif '平均成交额（亿元）' in item:
            value = float(item['平均成交额（亿元）'])
        elif '相关度分数' in item:
            value = float(item['相关度分数'])
        
        if label and value is not None:
            labels.append(label)
            values.append(value)
    
    if len(labels) < 2:
        return None
    
    # 创建SVG图表
    width = 400
    height = 300
    bar_width = 30
    bar_spacing = 10
    max_value = max(values) if values else 1
    
    svg = f'''<svg width="{width}" height="{height}" xmlns="http://www.w3.org/2000/svg">
    <rect width="{width}" height="{height}" fill="white"/>
    <text x="{width//2}" y="20" text-anchor="middle" font-family="Arial" font-size="14" font-weight="bold">{title}</text>'''
    
    for i, (label, value) in enumerate(zip(labels, values)):
        x = 50 + i * (bar_width + bar_spacing)
        bar_height = (value / max_value) * 200 if max_value > 0 else 0
        y = height - 50 - bar_height
        
        # 绘制柱状图
        svg += f'''
    <rect x="{x}" y="{y}" width="{bar_width}" height="{bar_height}" fill="lightblue" stroke="black"/>
    <text x="{x + bar_width//2}" y="{y - 5}" text-anchor="middle" font-family="Arial" font-size="10">{value:.2f}</text>
    <text x="{x + bar_width//2}" y="{height - 10}" text-anchor="middle" font-family="Arial" font-size="8">{label[:6]}</text>'''
    
    svg += '''
</svg>'''
    
    return svg
